<template>
  <div>
    <!-- 3.使用isAdd，展示添加还是编辑 -->
    <el-dialog :title="info.isAdd?'角色添加':'角色编辑'" :visible.sync="info.isshow" @closed="closed">
      {{form}}
      <el-form :model="form">
        <el-form-item label="角色名称" label-width="120px">
          <el-input v-model="form.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色权限" label-width="120px">
          <!-- 树形控件 -->
          <el-tree
            :data="menuList"
            :props="{children: 'children',label: 'title',}"
            node-key="id"
            ref="tree"
            show-checkbox
          ></el-tree>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  reqmenulist,
  reqroleadd,
  reqroleedit,
  reqroleinfo,
} from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  components: {},
  mounted() {},
  props: ["info", "list"],
  data() {
    return {
      // 1. 初始化数据
      form: {
        rolename: "",
        menus: "",
        status: "", //1正常2禁用   number类型
      },
      menuList: [], //菜单的list
    };
  },
  mounted() {
    // 2.请菜单list
    reqmenulist({ istree: true }).then((res) => {
      if (res.data.code == 200) {
        this.menuList = res.data.list;
      }
    });
  },
  methods: {
    //   4.取消
    cancel() {
      this.info.isshow = false;
    },
    //弹框关闭
    closed() {
      if (!this.info.isAdd) {
        this.empty();
      }
    },
    //重置
    empty() {
      this.form = {
        rolename: "",
        menus: "",
        status: "", //1正常2禁用   number类型
      };
      //清除树
      console.log(this.$refs.tree);
      this.$refs.tree.setCheckedKeys([]);
    },
    //添加
    add() {
      // （1）将树的数据给form.menus
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      // (2)数据交互
      reqroleadd(this.form).then((res) => {
        if (res.data.code == 200) {
          //成功了
          // （1）弹成功
          successAlert(res.data.msg);
          // (2)弹框消失
          this.cancel();
          // (3)清空数据
          this.empty();
          // (4)刷新列表
          this.$emit("init");
        }
      });
    },
    //修改了上级菜单
    changePid() {
      // 如果上级菜单是顶级菜单，就是目录，否则就是菜单
      if (this.form.pid == 0) {
        this.form.type = 1;
      } else {
        this.form.type = 2;
      }
    },
    //获取一条数据
    getOne(id) {
      reqroleinfo({ id: id }).then((res) => {
        if ((res.data.code = 200)) {
          this.form = res.data.list;
          //补id
          this.form.id = id;
          // 给树形控件赋值
          this.$refs.tree.setCheckedKeys(JSON.parse(this.form.menus));
        }
      });
    },
    // 修改
    update() {
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      reqroleedit(this.form).then((res) => {
        if (res.data.code == 200) {
          // （1）弹成功
          successAlert(res.data.msg);
          //如果修改的角色的id是当前用户的roleid,就需要重新登录
          if (this.form.id === this.user.roleid) {
            this.changeInfo({});
            this.$router.replace("/login");
            return;
          }
          // (2)弹框消失
          this.cancel();
          // (3)清空数据
          this.empty();
          // (4)刷新列表
          this.$emit("init");
        }
      });
    },
  },
};
</script>
<style lang='less' scoped>
</style>